
package ui;

import a4.Controller;
import a4.Main;
import user.Administrator;
import user.Customer;
import user.Employee;
        
class AdminUI extends UI{
    
    AdminUI( Administrator admin){
        user = admin;
    }
    
    public void adminMenu() {
        do {
          choise = UI.readString(
                      "1.Create user\n"
                    + "2.Update user\n"
                    + "3.Delete user\n"
                    + "4.Print employee list\n"
                    + "5.Print client list\n"
                    + "6.Logout\n "
                    + "What would you like to do? ","La8os elilogh","[1-6]");
            if (choise.equals("1"))
                createUser();
            else if (choise.equals("2"))
                updateUser();
            else if ( choise.equals("3") )
                deleteUser();
            else if ( choise.equals("4") )
                printEmployees();
            else if ( choise.equals("5") )
                printCustomers();
            else if ( choise.equals("6") ){
                logout();
                    return;}
            else
                System.out.println("Lathos epilogi");
        }
        while ( user.isLoggedIn() == true );
    }

    private void createUser() {
        String name, surname, email, password, telephone, address, userType;
        name = UI.readString("\nName: ", "Mh eggyro Onoma", "([A-Z][a-z])*");
        surname = UI.readString("\nSurname: ", "Mh eggyro Epi8eto", "([A-Z][a-z])*");
        do {
            email = UI.readString("\nEnter email: ","Mh eggyro email!","[A-Za-z][A-Za-z0-9.]*@[a-z]*(.[a-z]*)+");
            if ( Controller.emailExists(email) )
                System.out.println("\nThe provided email already exists.\nPlease, enter another email: ");
            else break;
        }
        while (true);
        telephone = readString("\nTelephone: ","Mh eggyro thlefwno","[0-9]{10}");
        address = readString("\nAddress: ","Parakalw eisagete thn pragmatikh dief8ynsh","([A-Z][a-z])*[0-9]*");
        password = readString("\nPassword: ","Password must be shorter than 10 characters and longer than three.\n","[A-Za-z0-9]{3,10}");
        userType = readString("\nType the type of the user you are creating: (A for Admininstrator, E for Employee, C for Customer","La8os epilogh","[AECaec]");
        if ( userType.equals("A") )
            Controller.addAdministrator(name, surname, email, password, telephone, address);
        else if ( userType.equals("E") )
            Controller.addEmployee(name, surname, email, password, telephone, address);
        else
            Controller.addCustomer(name, surname, email, password, email, address);
        System.out.println("\nThe user has successfully been created.\n");
    }

    private void updateUser() {
        String name, surname, password, telephone, address, email;
        boolean banned;
        
        printUserList();
        
        System.out.printf("Choose a users ID to update his credentials: ");
        int userID = input.nextInt();
        
        System.out.printf("\nName: %s\nType in the new name (empty means unchanged):", Main.userList.get(userID).getName());
        name = ( ( name = input.next() ).equals("") ? Main.userList.get(userID).getName() : name );

        System.out.printf("\nSurname: %s\nType in the new surname (empty means unchanged):", Main.userList.get(userID).getSurname());
        surname = ( ( surname = input.next() ).equals("") ? Main.userList.get(userID).getSurname() : surname ) ;
        
        System.out.printf("\ne-mail: %s\nType in the new e-mail (empty means unchanged) :", Main.userList.get(userID).getEmail());
        email = ( ( email = input.next() ).equals("") ? Main.userList.get(userID).getEmail() : email );
        
        System.out.printf("\nType in the new password (empty means unchanged):");
        password = ( ( password = input.next() ).equals("") ? Main.userList.get(userID).getPassword() : password );
        
        System.out.printf("\nTelephone: %s\nType in the new telephone (empty means unchanged):", Main.userList.get(userID).getEmail());
        telephone = ( ( telephone = input.next() ).equals("") ? Main.userList.get(userID).getTelephone() : telephone );
    
    }
    
    private void deleteUser() {
        printUserList();
        System.out.println("Provide the ID of the user : ");
        int epilogi = input.nextInt();
        Controller.deleteUser(epilogi);
    }
    
    private void printUserList(){
        for (int i=0; i<Main.userList.size();++i){
                System.out.print(i+" ");
            System.out.println(Main.userList.get(i).getEmail());}

    }
    
    private void printEmployees(){
        for (int i=0; i<Main.userList.size();++i)
            if (Main.userList.get(i) instanceof Employee){
                System.out.printf("%d. %10s %10s %10s ***** %10s %30s\n", i, 
                        Main.userList.get(i).getName(),
                        Main.userList.get(i).getSurname(),
                        Main.userList.get(i).getEmail(),
                        Main.userList.get(i).getTelephone(),
                        Main.userList.get(i).getAddress() );
            }
    }
    
    private void printCustomers(){
        for (int i=0; i<Main.userList.size();++i)
            if ( Main.userList.get(i) instanceof Customer ){
                System.out.printf("%d. %10s %10s %10s ***** %10s %30s\n", i, 
                        Main.userList.get(i).getName(),
                        Main.userList.get(i).getSurname(),
                        Main.userList.get(i).getEmail(),
                        Main.userList.get(i).getTelephone(),
                        Main.userList.get(i).getAddress() );
            }
    }
}
